package com.prs.crm.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.OrderBy;

import org.hibernate.annotations.Cascade;

import com.prs.crm.context.Constrains;
import com.prs.crm.domain.flow.FlowVerifyDetail;
import com.prs.crm.domain.hr.Person;

/**
 * 简单流程审核
 * 状态定义同一般流程
 * @author courser.tijichen
 *
 */

@SuppressWarnings("serial")
@MappedSuperclass
public abstract class SimpleFlowEntity extends BaseEntity{


	@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
	private Integer id;
	
	/**
	 * 审核人
	 */
	@ManyToOne
	private Person verifer;
	
	@OrderBy
	@ManyToMany(fetch= FetchType.LAZY)
	@Cascade({org.hibernate.annotations.CascadeType.ALL,org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
	private Set<FlowVerifyDetail> details;
	
	private Integer flowstatus;

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return id;
	}

	public void setVerifer(Person verifer) {
		this.verifer = verifer;
	}

	public Person getVerifer() {
		return verifer;
	}

	public void setDetails(Set<FlowVerifyDetail> details) {
		this.details = details;
	}

	public Set<FlowVerifyDetail> getDetails() {
		return details;
	}

	public void setFlowstatus(Integer flowstatus) {
		this.flowstatus = flowstatus;
	}

	public Integer getFlowstatus() {
		return flowstatus;
	}

	
	public void addVerifyDetail(FlowVerifyDetail detail)
	{
		if (details==null)
		{
			details = new HashSet<FlowVerifyDetail>();
		}
		details.add(detail);
	}
	
	/**
	 * 验证该实体是否已被锁定
	 * @return
	 */
	public  boolean getLocked()
	{
		if (this.getFlowstatus()!=null
				&& (this.getFlowstatus()==Constrains.WORKFLOWENTITY_STATE_PROCESS
						||
						this.getFlowstatus()==Constrains.WORKFLOWENTITY_STATE_VALID))
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
}
